<script type="text/javascript">
    $(function(){
        $("#search_grid").datagrid({
            url:"${base}/sys/yg/ygList",
            singleSelect:sfdx || false,
            toolbar:"#search_toolbar",
            pagination:true,
            fit:true,
            fitColumns:true,
            idField:'id',
            striped:true,
            columns:[[
                {field:'xm',title:'姓名',align:'center',width:8},
                {field:'gh',title:'工号',align:'center',width:8},
                {field:'zgjg_jc',title:'所属机构',align:'center',width:12}
            ]],
            onCheck: function(rowIndex,rowData) {
                if ($("#search_grid_selected").datagrid("getRowIndex",rowData.id) == -1)
                    $("#search_grid_selected").datagrid("insertRow",{row:rowData});
            },
            onUncheck:function(rowIndex,rowData) {
                var index = $("#search_grid_selected").datagrid("getRowIndex",rowData.id);
                if (index != -1){
                    $("#search_grid_selected").datagrid("deleteRow",index);
                }
            },
            onUncheckAll:function(rows){
                $.each(rows,function(index,value){
                    var index = $("#search_grid_selected").datagrid("getRowIndex",value);
                    if (index != -1)
                        $("#search_grid_selected").datagrid("deleteRow",index);
                });
            },
            onCheckAll:function(rows) {
                $.each(rows,function(index,value){
                    if ($("#search_grid_selected").datagrid("getRowIndex",value) == -1)
                        $("#search_grid_selected").datagrid("insertRow",{row:value});
                });
            },
            //初始化选择
            onLoadSuccess:function(data) {

            },
            //如果单选则响应双击事件
            onDblClickRow:function(rowIndex, rowData){
                if (!sfdx) {
                    alert("双击")
                }
            }
        });
        $("#search_grid_selected").datagrid({
            fit:true,
            idField:'id',
            fitColumns:true,
            columns:[[
                //{checkbox:true},
                {field:'xm',title:'姓名',align:'center',width:8},
                {field:'gh',title:'工号',align:'center',width:8},
                {field:'zgjg_jc',title:'所属机构',align:'center',width:12},
                {field:'id',align:'center',width:8,formatter:function(value,row,index){
                    return '<span style="color:red;"><a href="javascript:void(0)" class="search-row-del" onclick="deleteCurrentRow('+value+')">删除</a></span>';
                },title:'操作'}
            ]]
        });
        $("#search_tree").tree({
            onSelect:function(){
                $("#search_btn").click();
            },
            loadFilter:function(rows){
                return convert(rows);
            }
        });
        $("#search_btn").click(function(){
            var zgjgId = $("#search_tree").tree("getSelected") == null ? '' : $("#search_tree").tree("getSelected").id;
            if (zgjgId == $("#search_tree").tree("getRoot").id)
                zgjgId = '';
            $("#search_grid").datagrid("load",{
                xm:$("#input_search_xm").val(),
                zgjgId:zgjgId
            });
        });
    });
    //响应删除按钮，删除当前行
    function deleteCurrentRow(id) {
        var unSelectIndex = $("#search_grid").datagrid("getRowIndex",id);
        if(unSelectIndex != -1){
            $("#search_grid").datagrid("uncheckRow",unSelectIndex);
        } else {
            var grid = $('#search_grid').data("datagrid");
            var options = grid.options;
            var checkRows = grid.checkedRows;
            var selectedRows = grid.selectedRows;
            for(var i=0;i<checkRows.length;i++){
                if(checkRows[i][options.idField]==id){
                    checkRows.splice(i,1);
                }
            }
            for(var i=0;i<selectedRows.length;i++){
                if(selectedRows[i][options.idField]==id){
                    selectedRows.splice(i,1);
                }
            }
            $("#search_grid_selected").datagrid("deleteRow",$("#search_grid_selected").datagrid('getRowIndex',id));
        }
    }
</script>

<div class="easyui-layout" fit="true">
    <div region="west" split="true" style="width:180px;">
        <ul id="search_tree" class="easyui-tree" url="${base}/organ/gsjg/gsjg_tree" animate="true" line="true"></ul>
    </div>
    <div region="center" fit="false">
        <div class="easyui-layout" fit="true">
            <div region="center" fit="false">
                <div id="search_toolbar" style="height:auto">
                    <div class="textcon">
                        <table class="tb">
                            <tr>
                                <th>姓名:</th>
                                <td><input id="input_search_xm" type="text" class="input-text" style="width:100px;"/></td>
                                <td style=" padding-left:5px;"><input id="search_btn" type="button" value=""
                                                                      class="submit"/></td>
                            </tr>
                        </table>
                    </div>
                </div>
                <table id="search_grid"></table>
            </div>
            <div region="south" fit="false" split="true" style="height:150px;">
                <table id="search_grid_selected">
                </table>
            </div>
        </div>
    </div>
</div>
